package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.pojo.DtsGoods;
import com.qf.service.DtsGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/23 9:12
 */
@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {
    @Autowired
    private DtsGoodsMapper dtsGoodsMapper;
    @Override
    public Page<DtsGoods> selectPage(Integer page, Integer limit, String sort, String order, String name, Integer goodsSn) {
        Page<DtsGoods> dtsGoodsPage = new Page<>(page, limit);
        QueryWrapper<DtsGoods> dtsGoodsQueryWrapper = new QueryWrapper<>();
        dtsGoodsQueryWrapper.orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort).like(!StringUtils.isEmpty(name),"name",name).eq(goodsSn!=null,"goods_sn",goodsSn).eq("deleted",false);
        return dtsGoodsMapper.selectPage(dtsGoodsPage,dtsGoodsQueryWrapper);
    }

    @Override
    public DtsGoods selectByBrandId(Integer defaultCategoryId) {
        return dtsGoodsMapper.selectOne(new QueryWrapper<DtsGoods>().eq("category_id",defaultCategoryId));
    }

    @Override
    public DtsGoods selectById(Integer id) {
        return dtsGoodsMapper.selectById(id);
    }

    @Override
    public void update(DtsGoods goods) {
        dtsGoodsMapper.updateById(goods);
    }

    @Override
    public void delete(DtsGoods dtsGoods) {
        dtsGoodsMapper.updateById(dtsGoods);
    }

    @Override
    public void insert(DtsGoods goods) {
        dtsGoodsMapper.insert(goods);
    }

    @Override
    public DtsGoods selectByGoodsSn(String goodsSn) {
       return    dtsGoodsMapper.selectOne(new QueryWrapper<DtsGoods>().eq("goods_sn",goodsSn));
    }
}
